
global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global output_folder "W:\intimate\results_revision"

use "W:\intimate\data\allwomen_data_clean_withAnyViolence", clear 


// restrict to the matching sample in the end
merge 1:m shnro year_start_cohab using "W:\intimate\data\match_victim_data_clean_new", keepusing (shnro)
rename _merge merge_matched_dv 
duplicates drop shnro year_start_cohab, force


// restrict to the matching sample in the end
merge 1:m shnro year_start_cohab using "W:\intimate\data\match_MFvictim_data_clean_new", keepusing (shnro)
rename _merge merge_matched_mf 
duplicates drop shnro year_start_cohab, force

gen spouse_age=spouse_pre_age+1
gen spouse_college=spouse_pre_educ>=6
gen spouse_high_school=spouse_pre_educ>=3 & spouse_pre_educ<=5
gen spouse_ho=spouse_pre_educ<3

gen college=educ>=6
gen high_school=educ>=3 & educ<=5
gen ho=educ<3

ren spouse_pre_ptoim1 spouse_ptoim1B1
ren spouse_pre_tyotu spouse_tyotuB1
ren spouse_pre_nchild spouse_nchildB1
g spouse_pre_child_pos = (spouse_nchildB1>0)
g pre_child_pos = (nchildB1>0)

gen language=(kieli=="fi")
gen spouse_language=(spouse_kieli=="fi")

g year_event = year_start_cohab-1
merge 1:1 shnro year_event using "W:\intimate\data\cumul_crime_violent"
drop if _merge==2
replace cum_crimes = 0 if _merge==1
drop _merge sphnro
ren cum_crimes pre_crimes
g pre_crimes_pos = (pre_crimes >0)

merge m:1 shnro year_event using "W:\intimate\data\prescriptions_depression_indlevel.dta"
drop if _merge==2 
drop _merge 
replace depression=0 if depression==.
replace ndepression=0 if ndepression==. 
ren depression v_depression
ren ndepression v_ndepression

g sphnro = sphnro0
merge 1:1 sphnro year_event using "W:\intimate\data\cumul_crime_violent", keepusing(cum_crimes)
drop if _merge==2
replace cum_crimes = 0 if _merge==1
drop _merge sphnro
ren cum_crimes spouse_pre_crimes
g spouse_pre_crimes_pos = (spouse_pre_crimes >0)

g victim_id = shnro
replace shnro = sphnro0
merge m:1 shnro year_event using "W:\intimate\data\prescriptions_depression_indlevel.dta"
drop if _merge==2 
drop _merge 
replace depression=0 if depression==.
replace ndepression=0 if ndepression==. 
ren depression spouse_depression
ren ndepression spouse_ndepression
ren v_depression depression
ren v_ndepression ndepression

local varlist age college high_school ho depression ptoim1B1 tyotuB1 pre_child_pos language pre_crimes_pos
local table_varlist var_age var_college var_high_school var_ho var_depression var_ptoim1B1 var_tyotuB1 var_pre_child_pos var_language var_pre_crimes_pos

/* Create variables so easy to create table */
foreach thing in `varlist' {
    g var_`thing' = `thing'
}

eststo female_dv: estpost sum `table_varlist' if dv_couple==1 & merge_matched_dv==3

eststo female_nondv:  estpost sum   `table_varlist' if dv_couple==0 & victim_events==0 & suspect_events==0 & mf_crime==0 & year_start_cohab<=2014
						
drop var_*
foreach thing in `varlist' {
    g var_`thing' = spouse_`thing'
}
						
eststo male_dv: estpost sum   `table_varlist' if dv_couple==1 & merge_matched_dv==3
eststo male_nondv: estpost sum  `table_varlist' if dv_couple==0 & suspect_events==0 & victim_events==0 & mf_crime==0 & year_start_cohab<=2014

esttab female_dv male_dv female_nondv male_nondv using "$output_folder\Table1_basicdescrip.tex", replace main (mean %6.2f) aux(sd)  mtitle ("Victim" "Perpetrator" "Women" "Men") nonumber coeflabel (var_age "Age" var_college "College" var_high_school "High School" var_ho "Dropouts" var_ptoim1B1 "Employed at t-1" var_tyotuB1 "Earnings at t-1" var_pre_child_pos "Any Children" var_language "Speak Finnish" var_pre_crimes_pos "Prior Violent Crimes") title (Demographic Characteristics of New Cohabitees) nonotes addn (Notes: )



